﻿using RESTFinder.Service.Models;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Data.SQLite;

namespace RESTFinder.Service.Repositories
{
    public class DocumentRepository : CoreRepository<Models.DocumentModel>, IDocumentRepository
    {
        private const string LastWriteTimeKey = "LastWriteTime";
        public DocumentRepository()
        {

        }

        public bool Exist(string md5)
        {
            string sql = string.Format("select 1 from document where md5 = '{0}'", md5);
            var value = ExecuteScalarText(sql);

            return value != null;
        }

        public DocumentJsonModel Fetch(string md5)
        {
            string sql = string.Format("select * from document where md5 = '{0}'", md5);

            return  ExecuteReaderText<DocumentJsonModel>(sql, GetDocumentJson);
        }

        public List<DocumentJsonModel> Fetch(string[] md5s)
        {
            if (md5s.Length == 0) return null;
            string sql = string.Format("select * from document where md5 in ('{0}')", string.Join("','",md5s));

            return ExecuteReaderText<List<DocumentJsonModel>>(sql, GetDocumentJsons);
        }

        private List<DocumentJsonModel> GetDocumentJsons(IDataReader reader)
        {
            List<DocumentJsonModel> results = new List<DocumentJsonModel>();
            while (reader.Read())
            {
                results.Add(new DocumentJsonModel
                {
                    Md5 = reader["Md5"].ToString(),
                    FileName = reader["FileName"].ToString(),
                    FullPath = reader["FullPath"].ToString(),
                    Extension = reader["Extension"].ToString(),
                    ContentType = reader["ContentType"].ToString(),
                    RDate = reader["RDate"].ToString(),
                    LDate = reader["LDate"].ToString(),
                });
            }

            return results;
        }

        private DocumentJsonModel GetDocumentJson(IDataReader reader)
        {
            if (reader.Read())
            {
                return  new DocumentJsonModel
                {
                    Md5 = reader["Md5"].ToString(),
                    FileName = reader["FileName"].ToString(),
                    FullPath = reader["FullPath"].ToString(),
                    Extension = reader["Extension"].ToString(),
                    ContentType = reader["ContentType"].ToString(),
                    RDate = reader["RDate"].ToString(),
                    LDate = reader["LDate"].ToString(),
                };
            }
            return null;
        }

        public DateTime GetLastWriteTime()
        {
            string sql = string.Format("select Value from AppSetting where Key = '{0}'", LastWriteTimeKey);

            var value = ExecuteScalarText(sql);

            DateTime result = DateTime.MinValue;
            if (value == null)
            {
                return result;
            }

            if (DateTime.TryParse(value.ToString(), out result))
            {

            }

            return result;
        }

        public void SaveDocument(string md5, string name, string extension, string contentType, string filePath, DateTime realDate)
        {
            string sql = string.Format("INSERT INTO Document (Md5 ,FileName ,FullPath ,Extension ,ContentType ,RDate ,LDate )Values(@Md5 ,@FileName ,@FullPath ,@Extension ,@ContentType ,@RDate ,@LDate)");

            DbParameter[] parameters =
               {
                    new SQLiteParameter("@Md5", DbType.String) {Value = md5},
                    new SQLiteParameter("@FileName", DbType.String) {Value = name},
                    new SQLiteParameter("@FullPath", DbType.String) {Value = filePath},
                    new SQLiteParameter("@Extension", DbType.String) {Value = extension},
                    new SQLiteParameter("@ContentType", DbType.String) {Value = contentType},
                    new SQLiteParameter("@RDate", DbType.DateTime) {Value = realDate},
                    new SQLiteParameter("@LDate", DbType.DateTime) {Value = realDate},
                };

            Execute(sql, parameters);
        }
    }
}
